//
//  NSString+Timestamp.m
//  FZAH
//
//  Created by Yoga on 2017/4/17.
//  Copyright © 2017年 zdqk. All rights reserved.
//

#import "NSString+Timestamp.h"

@implementation NSString (Timestamp)

+ (NSString *)timeStampTransformToDate:(NSString *)timestamp {
    if (timestamp.length == 0 || [timestamp isEqualToString:@"0"]) {
        return @"";
    }
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timestamp integerValue]];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:date];
    
    NSString *year = [NSString stringWithFormat:@"%ld", (long)components.year];
    NSString *month = [NSString stringWithFormat:@"%ld", (long)components.month];
    NSString *day = [NSString stringWithFormat:@"%ld", (long)components.day];
    
    NSString *hour = [NSString stringWithFormat:@"%ld", (long)components.hour];
    NSString *minute = [NSString stringWithFormat:@"%ld", (long)components.minute];
    NSString *second = [NSString stringWithFormat:@"%ld", (long)components.second];
    
    if (components.month < 10) {
        month = [NSString stringWithFormat:@"0%ld", (long)components.month];
    }
    if (components.day < 10) {
        day = [NSString stringWithFormat:@"0%ld", (long)components.day];
    }
    if (components.hour < 10) {
        hour = [NSString stringWithFormat:@"0%ld", (long)components.hour];
    }
    if (components.minute < 10) {
        minute = [NSString stringWithFormat:@"0%ld", (long)components.minute];
    }
    if (components.second < 10) {
        second = [NSString stringWithFormat:@"0%ld", (long)components.second];
    }
    return [NSString stringWithFormat:@"%@-%@-%@ %@:%@", year, month, day, hour, minute];
}

/**
 *  @brief 转换成 yyyy年mm月dd日
 */
+ (NSString *)timeStampTransformToYear:(NSString *)timestamp {
    if (timestamp.length == 0 || [timestamp isEqualToString:@"0"]) {
        return @"";
    }
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timestamp integerValue]];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:date];
    
    NSString *year = [NSString stringWithFormat:@"%ld", (long)components.year];
    NSString *month = [NSString stringWithFormat:@"%ld", (long)components.month];
    NSString *day = [NSString stringWithFormat:@"%ld", (long)components.day];
    
    NSString *hour = [NSString stringWithFormat:@"%ld", (long)components.hour];
    NSString *minute = [NSString stringWithFormat:@"%ld", (long)components.minute];
    NSString *second = [NSString stringWithFormat:@"%ld", (long)components.second];
    
    if (components.month < 10) {
        month = [NSString stringWithFormat:@"0%ld", (long)components.month];
    }
    if (components.day < 10) {
        day = [NSString stringWithFormat:@"0%ld", (long)components.day];
    }
    if (components.hour < 10) {
        hour = [NSString stringWithFormat:@"0%ld", (long)components.hour];
    }
    if (components.minute < 10) {
        minute = [NSString stringWithFormat:@"0%ld", (long)components.minute];
    }
    if (components.second < 10) {
        second = [NSString stringWithFormat:@"0%ld", (long)components.second];
    }
    return [NSString stringWithFormat:@"%@-%@-%@", year, month, day];
}


+ (NSString *)countTimeTransfoWithString:(NSString *)timeStr{
    NSString *text ;
    
    NSDate *createDate = [NSDate dateWithTimeIntervalSince1970:[timeStr integerValue]];
    NSTimeInterval timeCount = (-1) *[createDate timeIntervalSinceNow];
    
    NSInteger minute = timeCount / 60;
    NSInteger hour = timeCount / (60 * 60);
    NSInteger day = timeCount / (60 * 60 * 24);
    NSInteger month = timeCount / (60 * 60 * 24 * 30);
    NSInteger year = timeCount / (60 * 60 * 24 * 365);
    
    if (minute <= 1) {
        text = @"刚刚而已";
    } else if ((minute > 1) && (hour < 1)) {
        text = [NSString stringWithFormat:@"%ld分钟前", (long)minute];
    } else if ((hour >= 1) && (day < 1)) {
        text = [NSString stringWithFormat:@"%ld小时前", (long)hour];
    } else if ((day >= 1) && (day <= 3) && (month < 1)) {
        text = [NSString stringWithFormat:@"%ld天前", (long)day];
    } else {
        
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"yyyy-MM-dd HH:MM"];//@"yyyy-MM-dd-HHMMss"
        NSString* dateString = [formatter stringFromDate:createDate];
        text = dateString;
    }
//    else if ((month >= 1) && (year < 1)) {
//        text = [NSString stringWithFormat:@"%ld月前", (long)month];
//    } else {
//        text = [NSString stringWithFormat:@"%ld年前", (long)year];
//    }
    
    return text;
}

/**
 *  @brief 剩余时间
 */
+ (NSString *)getTimeRemainingWithString:(NSString *)timeStr {
    NSString *text ;
    
    NSDate *createDate = [NSDate dateWithTimeIntervalSince1970:[timeStr integerValue]];
    NSTimeInterval timeCount = [createDate timeIntervalSinceNow];
    
    NSInteger minute = timeCount / 60;
    NSInteger hour = timeCount / (60 * 60);
    NSInteger day = timeCount / (60 * 60 * 24);
    NSInteger month = timeCount / (60 * 60 * 24 * 30);
    NSInteger year = timeCount / (60 * 60 * 24 * 365);
    
    if (timeCount < 0) {
        return @"0秒";
    }
    if (minute <= 1) {
        text = [NSString stringWithFormat:@"%ld秒", (long)timeCount]; //  @"";
    } else if ((minute > 1) && (hour < 1)) {
        text = [NSString stringWithFormat:@"%ld分钟", (long)minute];
    } else if ((hour >= 1) && (day < 1)) {
        text = [NSString stringWithFormat:@"%ld小时", (long)hour];
    } else if ((day >= 1) && (month < 1)) {
        text = [NSString stringWithFormat:@"%ld天", (long)day];
    } else if ((month >= 1) && (year < 1)) {
        text = [NSString stringWithFormat:@"%ld月", (long)month];
    } else {
        text = [NSString stringWithFormat:@"%ld年", (long)year];
    }
    
    return text;
}

@end
